<template>
  <div id="login">
    <el-row>
      <el-col :xs="1" :sm="4" :md="8" :lg="9">
        <div class="grid-content"></div>
      </el-col>
      <el-col :xs="22" :sm="16" :md="8" :lg="6">
        <div class="grid-content" style="margin-top: 10em;">
          <el-form
            :model="ruleForm"
            status-icon
            :rules="rules"
            ref="ruleForm"
            label-width="100px"
            class="demo-ruleForm"
          >
            <el-form-item label="用户名" prop="admin">
              <el-input
                type="text"
                v-model="ruleForm.admin"
                autocomplete="off"
                placeholder="请输入用户名"
              ></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="pass">
              <el-input
                type="password"
                v-model="ruleForm.pass"
                autocomplete="off"
                placeholder="请输入密码"
                @keyup.enter.native="submitForm('ruleForm')"
              ></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submitForm('ruleForm')" style="width: 47%">登录</el-button>
              <el-button @click="drawer = true" style="width: 47%">注册</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
      <el-col :xs="1" :sm="4" :md="8" :lg="9">
        <div class="grid-content"></div>
      </el-col>
    </el-row>

    <el-drawer
      size="70%"
      title="注册"
      :visible.sync="drawer"
      :direction="direction"
      :before-close="handleClose"
    >
      <div></div>

      <el-row>
        <el-col :xs="1" :sm="4" :md="8" :lg="9">
          <div class="grid-content"></div>
        </el-col>
        <el-col :xs="22" :sm="16" :md="8" :lg="6">
          <div class="grid-content">
            <el-form status-icon :model="formData" :rules="ruless" class="demo-ruleForm" ref="form">
              <el-form-item label="账号" prop="registerName">
                <el-input v-model="formData.registerName" placeholder="请输入账号" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item label="密码" prop="registerPwd">
                <el-input
                  v-model="formData.registerPwd"
                  type="password"
                  placeholder="请输入密码"
                  autocomplete="off"
                  @keyup.enter.native="register('form')"
                ></el-input>
              </el-form-item>
              <el-form-item>
                <el-button style="width: 100%;" type="primary" @click="register('form')">注册</el-button>
              </el-form-item>
            </el-form>
          </div>
        </el-col>
        <el-col :xs="1" :sm="4" :md="8" :lg="9">
          <div class="grid-content"></div>
        </el-col>
      </el-row>
    </el-drawer>
  </div>
</template>

<script>
export default {
  name: "login",
  data() {
    const admin = [
      {
        user: "admin",
        pass: "admin"
      }
    ];
    const validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.pass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    const validateAdmin = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入用户名"));
      } else {
        callback();
      }
    };
    const checkUser = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入账号"));
      } else if (
        this.admin.some(item => {
          return item.user === value;
        })
      ) {
        callback(new Error("账号已存在"));
      } else {
        callback();
      }
    };
    const checkPwd = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        pass: "",
        admin: ""
      },
      rules: {
        pass: [{ validator: validatePass, trigger: "blur" }],
        admin: [{ validator: validateAdmin, trigger: "blur" }]
      },
      ruless: {
        registerName: [{ validator: checkUser, trigger: "blur" }],
        registerPwd: [{ validator: checkPwd, trigger: "blur" }]
      },
      admin,

      drawer: false,
      direction: "btt",
      formData: {
        registerName: "",
        registerPwd: ""
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          if (
            this.admin.some(item => {
              return (
                item.user === this.ruleForm.admin &&
                item.pass === this.ruleForm.pass
              );
            })
          ) {
            this.$router.push({
              name: "home",
              params: { name: this.ruleForm.admin }
            });
          } else {
            this.$message("用户名或密码错误！");
            this.$refs.ruleForm.resetFields();
          }
        } else {
          this.$refs.ruleForm.resetFields();
          return false;
        }
      });
    },
    handleClose(done) {
      done();
    },
    register(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.admin.push({
            user: this.formData.registerName,
            pass: this.formData.registerPwd
          });
          this.drawer = false;
          this.$message("注册成功!");
          this.$refs.form.resetFields();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    keyupf() {
      console.log("enter")
    }
  }
};
</script>

<style scoped>
.grid-content {
  min-height: 36px;
}
#login {
  background-image: linear-gradient(-90deg, #29bdd9 0%, #276ace 100%);
  height: 100%;
  width: 100%;
}
</style>